package io.tec.cloud.algorithm.c03_v20220607;


import java.util.Queue;
import java.util.Stack;
import java.util.concurrent.LinkedBlockingDeque;

/**
 * 双向链表实现栈和队列
 */
public class Code03_DoubleNodeToStackAndQueue {

    public static void main(String[] args) {
//        MyStack<Integer> myStack = new MyStack<>();
//        myStack.push(5);
//        System.out.println(myStack.peek());
//        myStack.push(8);
//        System.out.println(myStack.peek());
//        myStack.push(7);
//        System.out.println(myStack.peek());
//        myStack.push(3);
//        System.out.println(myStack.peek());
//        myStack.push(4);
//        System.out.println(myStack.peek());
//        myStack.push(1);
//        System.out.println(myStack.peek());
//        myStack.push(2);
//        System.out.println(myStack.peek());
//        System.out.println(myStack.pop());
//        System.out.println(myStack.peek());
//        System.out.println(myStack.pop());
//        System.out.println(myStack.peek());
//        System.out.println(myStack.pop());
//        System.out.println(myStack.peek());
//        System.out.println(myStack.pop());
//        System.out.println(myStack.peek());
//        System.out.println(myStack.pop());
//        System.out.println(myStack.peek());
//        System.out.println(myStack.pop());
//        System.out.println(myStack.peek());
//        System.out.println(myStack.pop());

//        MyQueue<Integer> myQueue = new MyQueue<>();
//        myQueue.offer(5);
//        System.out.println(myQueue.peek());
//        myQueue.offer(8);
//        System.out.println(myQueue.peek());
//        myQueue.offer(7);
//        System.out.println(myQueue.peek());
//        myQueue.offer(3);
//        System.out.println(myQueue.peek());
//        myQueue.offer(4);
//        System.out.println(myQueue.peek());
//        myQueue.offer(1);
//        System.out.println(myQueue.peek());
//        myQueue.offer(2);
//        System.out.println(myQueue.peek());
//        System.out.println();
//        System.out.println(myQueue.peek());
//        System.out.println(myQueue.poll());
//        System.out.println(myQueue.peek());
//        System.out.println(myQueue.poll());
//        System.out.println(myQueue.peek());
//        System.out.println(myQueue.poll());
//        System.out.println(myQueue.peek());
//        System.out.println(myQueue.poll());
//        System.out.println(myQueue.peek());
//        System.out.println(myQueue.poll());
//        System.out.println(myQueue.peek());
//        System.out.println(myQueue.poll());
//        System.out.println(myQueue.peek());
//        System.out.println(myQueue.poll());

    }

    static class MyQueue<E> {

        DoubleNode<E> head;
        DoubleNode<E> tail;
//        LinkedBlockingDeque.Node<E> l = last;
//        node.prev = l;
//        last = node;
//        if (first == null)
//        first = node;
//        else
//        l.next = node;

        public E offer(E e) {
            DoubleNode node = new DoubleNode(e);
            if (head == null) {
                head = node;
                tail = node;
                return e;
            }
            node.last = tail;
            if (tail != null) {
                tail.next = node;
                tail = tail.next;
            }
            return e;
        }

        public E poll() {
            if (tail != null) {
                final E data = tail.data;
                tail = tail.last;
                return data;
            }
            return null;
        }

        public E peek() {
            if (tail != null) {
                final E data = tail.data;
                return data;
            }
            return null;
        }
    }

    /**
     * last-in-first-out (LIFO) stack
     */
    static class MyStack<E> {

        DoubleNode<E> head;
        DoubleNode<E> tail;

        public E push(E e) {
            DoubleNode node = new DoubleNode();
            node.data = e;
            if (tail != null) {
                tail.next = node;
                node.last = tail;
            } else {
                head = node;
            }
            tail = node;
            return e;
        }

        public E pop() {
            if (tail != null) {
                E data = tail.data;
                tail = tail.last;
                return data;
            }
            return null;
        }

        public E peek() {
            if (tail != null) {
                return tail.data;
            }
            return null;
        }
    }


}
